@echo off
setlocal enabledelayedexpansion

REM Property Cleaning System - Docker MySQL Windows Initialization Script
REM This script starts MySQL and initializes the database schema

echo ========================================
echo   Property Cleaning System - MySQL Setup
echo ========================================
echo.

REM Configuration
set CONTAINER_NAME=property-mysql
set ROOT_PASSWORD=root123
set DB_NAME=property_cleaning
set DB_USER=property_user
set DB_PASSWORD=property_pass
set PORT=3306
set SCHEMA_FILE=database\schema.sql

REM Check if Docker is running
docker --version >nul 2>&1
if errorlevel 1 (
    echo Error: Docker is not installed or not in PATH
    pause
    exit /b 1
)

docker info >nul 2>&1
if errorlevel 1 (
    echo Error: Docker is not running
    echo Please start Docker Desktop first
    pause
    exit /b 1
)

REM Check if schema file exists
if not exist "%SCHEMA_FILE%" (
    echo Error: Schema file not found: %SCHEMA_FILE%
    echo Please ensure database\schema.sql exists
    pause
    exit /b 1
)

echo Starting MySQL container...

REM Stop and remove existing container
docker stop %CONTAINER_NAME% >nul 2>&1
docker rm %CONTAINER_NAME% >nul 2>&1

REM Start MySQL container with schema volume
docker run -d ^
    --name %CONTAINER_NAME% ^
    -p %PORT%:3306 ^
    -e MYSQL_ROOT_PASSWORD=%ROOT_PASSWORD% ^
    -e MYSQL_DATABASE=%DB_NAME% ^
    -e MYSQL_USER=%DB_USER% ^
    -e MYSQL_PASSWORD=%DB_PASSWORD% ^
    -v %CD%\database:/docker-entrypoint-initdb.d ^
    mysql:8.0 ^
    --default-authentication-plugin=mysql_native_password

echo.
echo Waiting for MySQL to initialize...

REM Wait for MySQL to be ready
set RETRY_COUNT=0
set MAX_RETRIES=30

:wait_loop
set /a RETRY_COUNT+=1
if %RETRY_COUNT% GEQ %MAX_RETRIES% (
    echo.
    echo Error: MySQL failed to start properly
    echo Check logs with: docker logs %CONTAINER_NAME%
    pause
    exit /b 1
)

docker exec %CONTAINER_NAME% mysqladmin ping -h localhost -u root -p%ROOT_PASSWORD% --silent >nul 2>&1
if errorlevel 1 (
    <nul set /p=.
    timeout /t 2 /nobreak >nul
    goto wait_loop
)

echo.
echo MySQL is ready!
echo Importing database schema...

REM Import schema
docker exec -i %CONTAINER_NAME% mysql -uroot -p%ROOT_PASSWORD% %DB_NAME% < %SCHEMA_FILE%

echo Verifying database setup...

REM Get table count
for /f "tokens=*" %%a in ('docker exec %CONTAINER_NAME% mysql -uroot -p%ROOT_PASSWORD% -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='%DB_NAME%';" -s -N 2^>nul') do set TABLE_COUNT=%%a

echo Successfully created %TABLE_COUNT% tables

echo.
echo ========================================
echo MySQL setup completed successfully!
echo ========================================
echo.
echo Connection Information:
echo   Container: %CONTAINER_NAME%
echo   Port: %PORT%
echo   Database: %DB_NAME%
echo   User: %DB_USER%
echo   Password: %DB_PASSWORD%
echo.
echo Connection commands:
echo   From host: mysql -h127.0.0.1 -P%PORT% -u%DB_USER% -p%DB_PASSWORD% %DB_NAME%
echo   From container: docker exec -it %CONTAINER_NAME% mysql -u%DB_USER% -p%DB_PASSWORD% %DB_NAME%
echo.
echo Management commands:
echo   Check status: docker ps ^| find "%CONTAINER_NAME%"
echo   View logs: docker logs %CONTAINER_NAME%
echo   Stop: docker stop %CONTAINER_NAME%
echo   Start: docker start %CONTAINER_NAME%
echo.
echo ========================================
echo.
pause